package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.entity.PeriodSegment;
import com.yuyou.fn.educational.mapper.PeriodSegmentMapper;
import com.yuyou.fn.educational.service.IPeriodSegmentService;
import com.yuyou.fn.educational.vo.PeriodSegmentVo;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.*;

/**
 *
 * PeriodSegment 表数据服务层接口实现类
 *
 */
@Service
public class PeriodSegmentServiceImpl extends SuperServiceImpl<PeriodSegmentMapper, PeriodSegment,Long> implements IPeriodSegmentService {


    @Transactional
    @Override
    public void updateSegment(Long periodId, List<PeriodSegmentVo> segmentList) {
        if(periodId == null ){
            throw new NullPointerException();
        }
        Example<PeriodSegment> example = PeriodSegment.createExample();
        example.createCriteria().andEqualTo("periodId",periodId );
        deleteByExample(example);

        List<PeriodSegment> list = new ArrayList<>(segmentList.size());
        int i = 1 ;
        for(PeriodSegmentVo ps : segmentList ){
            PeriodSegment periodSegment = new PeriodSegment();
            BeanUtils.copyProperties(periodSegment, ps );
            periodSegment.setCreateTime(TimeUtils.now());
            periodSegment.setUpdateTime(TimeUtils.now());
            periodSegment.setPeriodId(periodId);
            periodSegment.setSegmentNo(i++);
            list.add(periodSegment);
        }
        insertBatch(list);

    }

    @Override
    public List<PeriodSegmentVo> findPeriodSegmentList(Long periodId) {
        Example<PeriodSegment> example = PeriodSegment.createExample();
        example.createCriteria().andEqualTo("periodId",periodId );
        example.orderBy("segmentNo");
        List<PeriodSegment> list = findListByExample(example);
        List<PeriodSegmentVo> retList  = new ArrayList<>(list.size());
        for(PeriodSegment ps  : list ){
            PeriodSegmentVo periodSegmentVo = new PeriodSegmentVo();
            BeanUtils.copyProperties(periodSegmentVo, ps );
            retList.add(periodSegmentVo);
        }
        return retList ;
    }

    @Override
    public List<PeriodSegment> findByPeriodSegmentListByPeriodIds(List<Long> periodIds) {
        if (Utils.checkListEmpty(periodIds)){
            return Collections.EMPTY_LIST;
        }
        Example<PeriodSegment> example = PeriodSegment.createExample();
        example.createCriteria().andIn("periodId",periodIds );
        List<PeriodSegment> list = findListByExample(example);
        return list ;
    }


    public   Map<Long,Map<Integer,PeriodSegment>> periodSegmentToMapGroup(List<PeriodSegment> periodSegmentList){
        Map<Long,Map<Integer,PeriodSegment>> result=new HashMap<>();
        if (Utils.checkListEmpty(periodSegmentList)){
            return result;
        }
        for (PeriodSegment periodSegment:periodSegmentList){
            Long periodId=periodSegment.getPeriodId();
            Integer segmentNo=periodSegment.getSegmentNo();
            Map<Integer,PeriodSegment> segmentMap=result.get(periodId);
            if (segmentMap==null){
                segmentMap=new HashMap<>();
                result.put(periodId,segmentMap);
            }
            segmentMap.put(segmentNo,periodSegment);
        }
        return result;
    }

    @Override
    public PeriodSegment findByIdAndNo(Long segmentId, Integer segmentNo) {
        Validate.notNull(segmentId);
        Validate.notNull(segmentNo);
        Example<PeriodSegment> example= PeriodSegment.createExample();
        example.createCriteria().andEqualTo("segmentId",segmentId).andEqualTo("segmentNo",segmentNo);
        return this.findOneByExample(example);
    }
}